# -*- coding:utf-8 -*-

from typing import List
from sqlalchemy.ext.asyncio import AsyncSession
from utils.common_util import CamelCaseUtil, export_list2excel
from module_admin.entity.vo.sys_table_vo import SysTablePageModel
from module_admin.service.sys_table_service import SysTableService
from utils.page_util import PageResponseModel
from module_vip.dao.bg_download_record_dao import BgDownloadRecordDao
from module_vip.entity.do.bg_download_record_do import BgDownloadRecord
from module_vip.entity.vo.bg_download_record_vo import BgDownloadRecordPageModel, BgDownloadRecordModel


class BgDownloadRecordService:
    """
    用户管理模块服务层
    """

    @classmethod
    async def get_bg_download_record_list(cls, query_db: AsyncSession, query_object: BgDownloadRecordPageModel, data_scope_sql: str) -> [list | PageResponseModel]:
        bg_download_record_list = await BgDownloadRecordDao.get_bg_download_record_list(query_db, query_object, data_scope_sql, is_page=True)
        return bg_download_record_list

    @classmethod
    async def get_bg_download_record_by_id(cls, query_db: AsyncSession, bg_download_record_id: int) -> BgDownloadRecordModel:
        bg_download_record = await  BgDownloadRecordDao.get_by_id(query_db, bg_download_record_id)
        bg_download_record_model = BgDownloadRecordModel(**CamelCaseUtil.transform_result(bg_download_record))
        return bg_download_record_model


    @classmethod
    async def add_bg_download_record(cls, query_db: AsyncSession, query_object: BgDownloadRecordModel) -> BgDownloadRecordModel:
        bg_download_record_model = await BgDownloadRecordDao.add_bg_download_record(query_db, query_object)
        return bg_download_record_model


    @classmethod
    async def update_bg_download_record(cls, query_db: AsyncSession, query_object: BgDownloadRecordModel) -> BgDownloadRecordModel:
        bg_download_record = await BgDownloadRecordDao.edit_bg_download_record(query_db, query_object)
        bg_download_record_model = BgDownloadRecordModel(**CamelCaseUtil.transform_result(bg_download_record))
        return bg_download_record_model


    @classmethod
    async def del_bg_download_record(cls, query_db: AsyncSession, bg_download_record_ids: List[str]):
        await BgDownloadRecordDao.del_bg_download_record(query_db, bg_download_record_ids)


    @classmethod
    async def export_bg_download_record_list(cls, query_db: AsyncSession, query_object: BgDownloadRecordPageModel, data_scope_sql) -> bytes:
        bg_download_record_list = await BgDownloadRecordDao.get_bg_download_record_list(query_db, query_object, data_scope_sql, is_page=False)
        filed_list = await SysTableService.get_sys_table_list(query_db, SysTablePageModel(tableName='bg_download_record'), is_page=False)
        filtered_filed = sorted(filter(lambda x: x["show"] == '1', filed_list), key=lambda x: x["sequence"])
        new_data = []
        for item in bg_download_record_list:
            mapping_dict = {}
            for fild in filtered_filed:
                if fild["prop"] in item:
                    mapping_dict[fild["label"]] = item[fild["prop"]]
            new_data.append(mapping_dict)
        binary_data = export_list2excel(new_data)
        return binary_data

    @classmethod
    async def get_download_statistics(cls, query_db: AsyncSession) -> dict:
        """
        获取下载统计信息
        """
        statistics = await BgDownloadRecordDao.get_download_statistics(query_db)
        return statistics